﻿@using NewLife.Cube.Entity
@using NewLife.Cube.Extensions
@using NewLife.Cube;
@using NewLife.Cube.ViewModels;
@using NewLife.Web;
@using XCode;
@using NewLife.Cube.Charts;
@using NewLife;
@using XCode.Configuration;
@using XCode.Membership;

@{
    var fact = ViewBag.Factory as IEntityFactory;
    var pager = ViewBag.Page as Pager;
    var set = ViewBag.PageSetting as PageSetting;

    var charts = ViewBag.Charts as ECharts[];
    var charts2 = ViewBag.Charts2 as ECharts[];

    var fields = ViewBag.Fields as FieldCollection;
    var ukey = fact.Unique;

    var act = Context.Request.Path + "";
    if (act.IsNullOrEmpty())
    {
        act = Url.Action("Index");
    }

    var an = ViewContext.RouteData.Values["action"].ToString().ToLower();

    var rv = pager.GetRouteValue();

    var hideSearch = @Context.Request.GetRequestValue("search") == "1" ? "layui-hide" : "";
}

@section css{
    <style>
        .MapProvider dd {
            text-align: left;
        }
    </style>
}
<div class="layui-fluid" id="mainpage">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                @await Html.PartialAsync("_List_Serch")
                <div class="layui-card-body">
                    <table lay-filter="parse-table" id="parse-table">
                        <thead>
                            <tr>
                                @if (set.EnableSelect)
                                {
                                    <th lay-data="{checkbox:true}"></th>
                                }
                                <th lay-data="{field:'Category',title:'类别',sort:true}">类别</th>
                                <th lay-data="{field:'UserName',title:'用户',sort:true}">用户</th>
                                <th lay-data="{field:'Action',title:'操作',sort:true}">操作</th>
                                <th lay-data="{field:'Success',title:'成功'}">成功</th>
                                <th lay-data="{field:'Remark',title:'详细信息',sort:true}">详细信息</th>
                                <th lay-data="{field:'LinkID',title:'链接',sort:true}">链接</th>
                                <th lay-data="{field:'CreateIP',title:'IP地址',sort:true}">IP地址</th>
                                @*<th lay-data="{field:'CreateAddress',title:'物理地址',sort:true}">物理地址</th>*@
                                <th lay-data="{field:'CreateTime',title:'时间',sort:true}">时间</th>
                                <th lay-data="{toolbar:'#nearOpt',align:'center'}">附近</th>
                                @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
                                {
                                    <th lay-data="{toolbar:'#barOpt',minWidth:180,fixed:'right',align:'center'}">操作</th>
                                }
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var entity in Model)
                            {
                                <tr>
                                    @if (set.EnableSelect)
                                    {
                                        <td lay-data="{checkbox:true}"></td>
                                    }
                                    <td>@entity.Category</td>
                                    <td>@entity.UserName</td>
                                    <td>@entity.Action</td>
                                    <td>
                                        <i class="layui-icon @(entity.Success ? "layui-icon-ok" : "layui-icon-close")" style="color: @(entity.Success ? "green" : "red");font-size:20px"></i>
                                    </td>
                                    <td style="overflow:hidden;white-space: nowrap;text-overflow: ellipsis;" title="@entity.Remark">@entity.Remark</td>
                                    <td>@entity.LinkID.ToString("n0")</td>
                                    <td>@entity.CreateIP</td>
                                    @*<td>@entity.CreateIP</td>*@
                                    <td>@entity.CreateTime</td>
                                    @*<td>
                                <a href="?act=near&id=@entity.ID&range=10" class="layui-btn layui-btn-primary layui-btn-xs" title="前后10行日志">附近</a>
                                </td>*@
                                </tr>
                            }
                        </tbody>
                    </table>
                    @await Html.PartialAsync("_List_Pager")
                </div>
            </div>
        </div>
    </div>
</div>

@section layuiscripts{
    @*        table.init('parse-table', { //转化静态表格
            page: true,
            limit: 20,
            limits: [20, 50, 100],
            even: true,
            defaultToolbar: ['filter','print',"exports",{title:'高级',layEvent: 'moreTool',icon: 'layui-icon-spread-left',id:"demo"}],
            toolbar: "#table-toolbar"
        });*@
                            var ii = layer.load(0, {shade: false});

                table.init('parse-table', { //转化静态表格
                    page: false,
                    limit: @pager.PageSize,
                    limits: [20, 50, 100],
                    even: true,
                    defaultToolbar: ['filter','print',"exports"],
                    toolbar: "#table-toolbar",
                    height: 'full-100',
                    cellMinWidth: 100,
                    filterUrl:"@Url.Action("Index")",
                    filterMethod:"from",
                    drag: {toolbar: true},
    @*            where:JSON.parse('@Html.Raw(Context.Request.GetRequestValue("filterSos"))'),*@
    @*where:{filterSos:'@Html.Raw(Context.Request.GetRequestValue("filterSos"))'},*@
                    filter: {
    @*items:['column','data','condition','editCondition','clearCache'],*@
                        items:['column','data','condition','clearCache'],
                        cache: false,
                        bottom: false
                    },
                    done: function () {
                        soulTable.render(this)
                    }
                });

                        layer.close(ii);
                        $("#parse-table,#listPager").removeClass("layui-hide");

                        //头工具栏事件
                        table.on('toolbar(parse-table)', function(obj){
                          var checkStatus = table.checkStatus(obj.config.id);
                          switch(obj.event){
                            case 'B_Detele_Data':
                            debugger;
                              var checkdata = checkStatus.data;
                              layer.confirm('该操作将删除选中数据并不可恢复！确认删除？', function(index){
                              $.get('@Url.Action("DeleteSelect")',{Keys:stringArray(checkdata)},function(res){
                                  layer.msg(res.message,{
                                           time:500,
                                           end:function () {
                                               if(res.code==0)
                                               location.href = '@Url.Action("Index")'
                                          }
                                  })
                              });
                              layer.close(index);
                            });
                            break;
                            case 'Add_Data':
                              location.href='@Url.Action("Add", rv)'
                            break;
                            case 'ImpExcel':
                                layer.open({
                                  type: 1,
                                  title: '导入excel',
                                  skin: 'layui-layer-admin',
                                  closeBtn: false,
                                  area: ['300px', '300px'],
                                  anim: 5,
                                  shadeClose: true,
                                  closeBtn:2,
                                  content: laytpl($('#list-imp-excel').html()).render({}),
                                  cancel : function() {
                                        layer.close();
                                  },
                                  success : function(layero, index) {
                                        ImportExcel();
                                  }
                                });
                            break;
                            case 'moreTool':
                            break;
                          };
                        });

    @if (this.Has(PermissionFlags.Detail))
    {
        @await Html.PartialAsync("DropDown/_Detail")
    }

    @if (this.Has(PermissionFlags.Delete))
    {
        @await Html.PartialAsync("DropDown/_Delete")
    }
    @if (this.Has(PermissionFlags.Update))
    {
        @await Html.PartialAsync("DropDown/_Update")
    }
    @if (this.Has(PermissionFlags.All))
    {
        @await Html.PartialAsync("DropDown/_All")
    }



                        //监听行工具事件
                        table.on('tool(parse-table)', function(obj){
                          var data = obj.data;

                          var data_id=data["@fact.Unique.Name"];

                          if(!data_id)
                            data_id=$(this).attr("value");

                          if(obj.event === 'del'){
                            layer.confirm('真的删除该数据吗', function(index){
                                   $.get('@Url.Action("Delete")/'+data_id,function(res){
                                    obj.del();
                                    layer.close(index);
                               });
                            });
                          }
                          else if(obj.event === 'edit'){
                              location.href = '@Url.Action("Edit")/'+data_id;
                          }
                          else if(obj.event === 'near'){
                              var contentUrl='@Url.Action("Index")?act=near&id='+obj.data.@fact.Unique.Name+'&range=10&search=1';
                              layer.open({
                                            type: 2,
                                            title: '查看前后10行日志',
                                            shadeClose: false,           //弹出框之外的地方是否可以点击
                                            offset: '10%',
                                            area: ['70%', '80%'],
                                            btn: ['确定','取消'],
                                            yes: function(index, layero){
    @*var iframe = window['layui-layer-iframe' + index]; // 获取弹出层iframe对象
                                            var data = iframe.getChooseData();   // 执行弹出页面层的方法getChooseData()，获取选中的数据
                                            if(data != false){
                                                $.post('@Url.Action("Authorized")/',{data:JSON.stringify(data),id:id},function(res){
                                                  layer.msg(res.message,{
                                                               time:3000,
                                                                 end:function () {
                                                                     if(res.code==0)
                                                                        location.href = '@Url.Action("Index")'
                                                                }
                                                             })
                                               });*@
                                                    layer.closeAll();
    @*}*@
                                            },btn2: function(){
                                             layer.closeAll();
                                            },
                                                    content: contentUrl
                                        });
                          }
                          else if(obj.event === 'select'){
                              location.href = '@Url.Action("Detail")/'+data_id;
                          }
                          else if(obj.event === 'action'){
                                var url=$(this).attr("lay-data");
                                var dataaction=$(this).attr("data-action");
                                if(dataaction)
                                {
                                    $.get(url,function(res){
                                          layer.msg(res.message,{
                                                       time:500,
                                                         end:function () {
                                                             if(res.code==0)
    @*location.href = '@Url.Action("Index")'*@
                                                                location.reload();
                                                                //同步更新缓存对应的值
    @*var newEnable="";
                                            if(data.Enable.lastIndexOf('=True')){
                                                newEnable=data.Enable.replace("=True","=False");
                                            }
                                            if(data.Enable.lastIndexOf('=False')){
                                                newEnable=data.Enable.replace("=False","=True");
                                            }*@


    @*data.Enable=newEnable;*@
    @* obj.update(data);
                                            table.reload("parse-table");*@
                                                        }
                                                     })
                                       });
                               }

                          }
                        });

                        //单击事件
                        table.on('row(parse-table)', function(obj){
                          var data = obj.data;
    @*console.info("单击");*@
                        });

                        //双击事件
                        table.on('rowDouble(parse-table)', function(obj){
                          var data = obj.data;
                          var jumpUrl='@Url.Action("Edit", rv)';
                          if(jumpUrl.indexOf('?')>-1){
                            jumpUrl=jumpUrl.slice(0,jumpUrl.indexOf('?'));
                          }
                          debugger;
                            location.href = jumpUrl+'/'+data["@fact.Unique.Name"];
    });


}
@section scripts{
    <script type="text/html" id="nearOpt">
        <a lay-event="near" class="layui-btn layui-btn-primary layui-btn-xs" title="前后10行日志">附近</a>
    </script>
    @await Html.PartialAsync("_List_Toolbar")

    @* 设置行操作按钮*@
    @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
    {
        @foreach (var entity in Model)
        {
            @await Html.PartialAsync("_List_Data_Action", (Object)entity)
            break;
        }
    }

    @* 设置行状态按钮*@
    @if (this.Has(PermissionFlags.Update))
    {
        @foreach (var entity in Model)
        {
            @await Html.PartialAsync("_List_Data_Switch", (Object)entity)
            break;
        }
    }

    @await Html.PartialAsync("_List_Data_Imp_Excel")
}

<script>
    function stringArray(checkdata) {
        var get_data = new Array();
        //拼接字符串
        for (var i = 0; i < checkdata.length; i++) {
            if (checkdata[i].@fact.Unique) {
                get_data.push(checkdata[i].@fact.Unique);
            }
        }

        return get_data.join(',');
    }
</script>
